<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="add(0,1)">添加权限</el-button>
      <el-table :data="list" row-key="id">
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button v-if="row.type === 1" size="mini" type="text" @click="add(row.id,2)">添加</el-button>
            <el-button size="mini" type="text" @click="hEdit(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 添加  弹出框 -->
    <el-dialog width="500px" :title="`${showText}权限点`" :visible.sync="showDialog">
      <!-- 表单内容 -->
      <el-form ref="formData" label-width="120px" :model="formData" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" :rows="3" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" type="textarea" :rows="3" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item label="启用">
          <el-switch
            v-model="formData.enVisible"
            active-color="#13ce66"
            inactive-color="#ff4949"
            size="mini"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="addOk">确定</el-button>
              <el-button size="mini" @click="hClose">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getPermission, addPermission, delPermission, getPermissionUser, updatePermission } from '@/api/permission'
import { transListToTreeData } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      showDialog: false,
      list: [],
      formData: {
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '',
        enVisible: '0'
      },
      // 表单校验  prop
      rules: {
        name: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑' : '新增'
    }
  },
  created() {
    this.getPermission()
  },
  methods: {
    // 回显
    async hEdit(id) {
      this.showDialog = true
      const res = await getPermissionUser(id)
      this.formData = res.data
    },
    // 删除
    async del(id) {
      await delPermission(id)
      this.$message.success('删除成功')
      this.getPermission()
    },
    // 页面的渲染
    async getPermission() {
      const res = await getPermission()
      console.log(res)
      this.list = transListToTreeData(res.data, 0)
    },
    add(pid, type) {
      this.formData.pid = pid
      this.formData.type = type
      this.showDialog = true
    },
    // 添加
    async addPer() {
      await addPermission(this.formData)
      this.$message.success('添加成功!')
      this.hClose()
      this.getPermission()
    },
    async updatePer() {
      await updatePermission(this.formData)
      this.$message.success('修改成功!')
      this.hClose()
      this.getPermission()
    },
    // 拖地校验
    //
    addOk() {
      this.$refs.formData.validate(valid => {
        valid && this.formData.id ? this.updatePer() : this.addPer()
      })
    },
    hClose() {
      this.showDialog = false
      // 清空表单
      this.formData = {
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '',
        enVisible: '0'
      }
    }
  }
}
</script>
<style>
.btn-add {
  margin: 10px;
}
</style>
